package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TUserAddress;
import com.tencent.wxcloudrun.mapper.TUserAddressMapper;
import com.tencent.wxcloudrun.service.TUserAddressService;

/**
 * @author ziyin.zhao
 * @description 针对表【t_user_address(用户地址表)】的数据库操作Service实现
 * @createDate 2025-04-22 16:43:56
 */
@Service
public class TUserAddressServiceImpl extends ServiceImpl<TUserAddressMapper, TUserAddress>
    implements TUserAddressService {

    @Override
    public void deleteByUserId(Long userId) {
        LambdaUpdateWrapper<TUserAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TUserAddress::getDeleteFlag, 1);
        wrapper.set(TUserAddress::getEnableFlag, 0);
        wrapper.set(TUserAddress::getUpdatedTime, LocalDateTime.now());
        wrapper.set(TUserAddress::getUpdatedBy, userId);
        wrapper.eq(TUserAddress::getUserId, userId);
        this.update(wrapper);
    }

    @Override
    public TUserAddress queryByUserId(Long userId) {
        LambdaQueryWrapper<TUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUserAddress::getUserId, userId);
        queryWrapper.eq(TUserAddress::getEnableFlag, 1);
        queryWrapper.orderByDesc(TUserAddress::getUpdatedTime);
        List<TUserAddress> addressList = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(addressList)) {
            return addressList.get(0);
        }
        return null;
    }
}
